package controllers;

public class OutExcel extends Controller{
	
	// 导出excel
	public static void outExcel() {
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		System.out.println("进来了+++++");

		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
		String lastlogintime = formatter.format(curDate);

		System.out.println(lastlogintime);
		;

		// Excel 文件存放在服务器的相对路径下
		String outputFile = "d://" + lastlogintime + ".xls";

		HSSFWorkbook workbook = null;
		try {
//			String eid = request.getParameter("eid");
			// System.out.println(eid);
			// String hql1="from ExamTopic exam.eid='"+eid+"'";
			// System.out.println(hql1);
			// topic=(ExamTopic) daoMannger.loadObject(hql1);

			// outputFile = request.getRealPath("/exam/tmp/topic.xls");
			System.out.println(outputFile);

			// 创建新的Excel 工作簿
			workbook = new HSSFWorkbook();
			// 在Excel 工作簿中建一工作表
			HSSFSheet sheet = workbook.createSheet("Sheet1");
			sheet.setDefaultColumnWidth((short) 20);
			// 设置单元格格式(文本)
			HSSFCellStyle cellStyle = workbook.createCellStyle();
			cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("@"));

			// 在索引0的位置创建行（第一行）
			HSSFRow row = sheet.createRow((short) 0);
			HSSFCell cell1 = row.createCell((short) 0);// 第一列
			HSSFCell cell2 = row.createCell((short) 1);
			HSSFCell cell3 = row.createCell((short) 2);
			HSSFCell cell4 = row.createCell((short) 3);
			HSSFCell cell5 = row.createCell((short) 4);
			HSSFCell cell6 = row.createCell((short) 5);
			// 定义单元格为字符串类型
			cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell6.setCellType(HSSFCell.CELL_TYPE_STRING);

			cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
			// 在单元格中输入数据
			cell1.setCellValue("题目标题");
			cell2.setCellValue("类型"); // 1.单选2.多选4.判断
			cell3.setCellValue("难度系数");
			cell4.setCellValue("知识点");
			cell5.setCellValue("题目内容");
			cell6.setCellValue("选项");

//			String hql = "from ExamTopic where exam.eid='" + eid + "'";
//			topicList = daoMannger.query(hql);
			Indent indent = Indent.

			int j = 1;
			for (ExamTopic t : topicList) {
				row = sheet.createRow((short) j);
				cell1 = row.createCell((short) 0);
				cell2 = row.createCell((short) 1);
				cell3 = row.createCell((short) 2);
				cell4 = row.createCell((short) 3);
				cell5 = row.createCell((short) 4);
				cell6 = row.createCell((short) 5);
				// 定义单元格为字符串类型
				cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell6.setCellType(HSSFCell.CELL_TYPE_STRING);

				cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell6.setEncoding(HSSFCell.ENCODING_UTF_16);

				cell1.setCellValue(t.getTopicName());
				cell2.setCellValue(t.getType());
				cell3.setCellValue(t.getDegreeOfDifficulty());
				cell4.setCellValue(t.getTag());
				cell5.setCellValue(t.getContent());
				String xx = "";
				for (ExamTopicOption o : t.getOptions()) {
					xx += "{`#`}" + o.getCode() + "{``}" + o.getContent()
							+ "{``}" + o.getType();
				}
				if (xx.length() > 5) {
					cell6.setCellValue(xx.substring("{`#`}".length(), xx
							.length()));
				} else {
					cell6.setCellValue("");
				}
				j = j + 1;
			}
			//response.getWriter().print("1");
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 删除路径下同名的Excel 文件
		File path = new File(outputFile);
		path.delete();

		// 新建一输出文件流
		FileOutputStream fOut;
		try {
			fOut = new FileOutputStream(outputFile);
			// 把相应的Excel 工作簿存盘
			workbook.write(fOut);
			// 操作结束，关闭文件
			fOut.flush();
			fOut.close();
			// 该处如果Excel过大会影响效率，谁有好的想法可以提出来参考（不过从页面下载完后就会清空）
			request.getSession().setAttribute("Download", outputFile);
			
			
			// 打开指定文件的流信息  
	        InputStream is = new FileInputStream(outputFile);  
	        // 写出流信息  
	        int data = -1;  
	        OutputStream outputstream = response.getOutputStream();  
	  
	        // 清空输出流  
	        response.reset();  
	        // 设置响应头和下载保存的文件名  
	        response.setHeader("content-disposition", "attachment;filename="+lastlogintime + ".xls");  
	        // 定义输出类型  
	        response.setContentType("APPLICATION/msexcel;charset=utf-8");  
	  
	        while ((data = is.read()) != -1)  
	            outputstream.write(data);  
	        is.close();  
	        outputstream.close();  
	        response.flushBuffer();  
	        
	        path.delete();
	        
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
}